package com.bju.controller;

import com.bju.service.PassPortService;
import com.bju.utils.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

import java.net.URL;
import java.util.UUID;


@RestController
@RequestMapping("/passport")
public class PassportController {

    @Resource
    private PassPortService passPortService;

    @RequestMapping("/path")
    public Result getPassportMessage(@RequestParam("file")MultipartFile file){
        if (file.isEmpty()) {
            return Result.fail(530,"上传文件为空");
        }
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 生成新的文件名
            String newFileName = UUID.randomUUID().toString() + suffixName;
            // 设置文件保存路径
            String filePath = "E:\\workspace\\passport-project-server\\temp\\" + newFileName;
            // 将文件保存到本地
            File dest = new File(filePath);
            file.transferTo(dest);
            // 返回文件访问URL
//            String fileUrl = "http://localhost:8200/" + newFileName;
            String passportMessage = passPortService.getPassportMessage(filePath);
            System.out.println("-----------------"+passportMessage);
            return Result.success("图片插入正常",passportMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(520,"图片插入失败");
        }
    }



}
